package com.cskaoyan.market.service.wx;/*
 *文件名: WxCategoryServiceImpl
 *创建者:wxin
 *创建时间:2024/5/20 14:39
 */


import com.cskaoyan.market.bo.GoodBo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public MarketGoods getGoodsById(Integer id) {
        return marketGoodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MarketGoods> queryByCategoryId(Integer currentGoodsID, Integer categoryId, Integer head, Integer limited) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        List<MarketGoods> result = new ArrayList<>();
        if (marketGoods.size() < limited) {
            for (MarketGoods marketGood : marketGoods) {
                //如果查询到的同品类商品与当前商品是同一个，则不添加此商品
                if (!marketGood.getId().equals(currentGoodsID)) {
                    result.add(marketGood);
                }
            }
            return result;
        }
        for (int i = head; i < limited; i++) {
            MarketGoods goods = marketGoods.get(i);
            if (goods.getId().equals(currentGoodsID)) {
                //如果查询到的同品类商品与当前商品是同一个，则往后再查询一个
                limited = limited + 1;
                continue;
            }
            result.add(goods);
        }
        return result;
    }

    @Override
    public Map<String, Object> goodsCategory(String id) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample marketBroExample = new MarketCategoryExample();
        MarketCategoryExample marketCurrentExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteriaCur = marketCurrentExample.createCriteria();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        MarketCategoryExample.Criteria criteriaBro = marketBroExample.createCriteria();
        Map<String, Object> goodsCategoryMap = new HashMap<>();
        MarketCategory reqCategory = marketCategoryMapper.selectByPrimaryKey(Integer.valueOf(id));
        MarketCategoryExample marketCategoryExampleParent = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteriaParent = marketCategoryExampleParent.createCriteria();
        MarketCategory parentCategory = new MarketCategory();
        //如果传入的category的类型是一级类目，brother里存的就是二级类目
        //如果传入的category的类型是二级类目，brother里存的就是兄弟类目
        //定义了一个currentCategory
        List<MarketCategory> brotherCategoryList = new ArrayList<>();
        MarketCategory currentCategory = null;
        if (!StringUtils.isEmpty(id)) {
            //如果传入的category的类型是一级类目，brother里存的就是二级类目
            //current就是第一个
            if (reqCategory.getPid() == 0) {
                criteriaBro.andPidEqualTo(Integer.valueOf(id));
                brotherCategoryList = marketCategoryMapper.selectByExample(marketBroExample);
                currentCategory = marketCategoryMapper.selectByPrimaryKey(brotherCategoryList.get(0).getId());
                parentCategory = marketCategoryMapper.selectByPrimaryKey(reqCategory.getId());


            }
            //如果传入的category的类型是二级类目，brother里存的就是兄弟类目
            //cur就是当前的
            else {
                criteriaBro.andPidEqualTo(Integer.valueOf(reqCategory.getPid()));
                brotherCategoryList = marketCategoryMapper.selectByExample(marketBroExample);
                currentCategory = marketCategoryMapper.selectByPrimaryKey(reqCategory.getId());
                parentCategory = marketCategoryMapper.selectByPrimaryKey(reqCategory.getPid());

            }
        }
        goodsCategoryMap.put("brotherCategory", brotherCategoryList);
        goodsCategoryMap.put("currentCategory", currentCategory);

        goodsCategoryMap.put("parentCategory", parentCategory);
        return goodsCategoryMap;

    }

    @Override
    public Integer goodsCount() {
        MarketGoodsExample example = new MarketGoodsExample();
        example.or().andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        return (int) marketGoodsMapper.countByExample(example);

    }


    @Override
    public List<MarketGoods> goodsList1(GoodBo goodBo) {
        Integer limitParam = goodBo.getLimit();
        Integer pageParam = goodBo.getPage();
        String categoryId = goodBo.getCategoryId();
        String keyword = goodBo.getKeyword();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (goodBo.getSort() != null && goodBo.getOrder() != null) {
            marketGoodsExample.setOrderByClause(goodBo.getSort() + " " + goodBo.getOrder());
        }
        List<MarketGoods> list = null;
        if (StringUtils.isEmpty(keyword)) {
            if (!StringUtils.isEmpty(categoryId)) {

                criteria.andCategoryIdEqualTo(Integer.valueOf(categoryId));
            }
            PageHelper.startPage(pageParam, limitParam);
            list = marketGoodsMapper.selectByExample(marketGoodsExample);
        }
        //关键词查找
        else {
            if (Integer.valueOf(categoryId) != 0) {
                criteria.andCategoryIdEqualTo(Integer.valueOf(categoryId));

                PageHelper.startPage(pageParam, limitParam);
                list = marketGoodsMapper.selectByExample(marketGoodsExample);


            } else {
                criteria.andNameLike("%" + keyword + "%");
                PageHelper.startPage(pageParam, limitParam);
                list = marketGoodsMapper.selectByExample(marketGoodsExample);
            }

        }
        return list;

    }

    @Override
    public List<MarketCategory> filterList1(GoodBo goodBo) {
        //先通过goodBo找到他的父id
        //再找到父id相同的分类
        //二级类目有商品就返回
//        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
//        List<MarketCategory> filterCategoryList=new ArrayList<>();
//        if(StringUtils.isEmpty(goodBo.getKeyword())) {
//            if (!StringUtils.isEmpty(goodBo.getCategoryId())) {
//                criteria1.andPidEqualTo(Integer.valueOf(goodBo.getCategoryId()));
//            }
//            filterCategoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
//        }
//        //根据关键词查找
//        else{
//            criteria1.andKeywordsLike("%"+goodBo.getKeyword()+"%");
//            //找到keyword对应的二级分类
//            List<MarketCategory> goodList=marketCategoryMapper.selectByExample(marketCategoryExample);
//            //找到keyWord对应的pid
//            for (int i = 0; i < goodList.size(); i++) {
//                MarketCategoryExample marketCategoryExample2 = new MarketCategoryExample();
//                MarketCategoryExample.Criteria criteria2 = marketCategoryExample2.createCriteria();
//                criteria2.andPidEqualTo(goodList.get(i).getPid());
////                criteria2.andIdEqualTo(goodList.get(i).getPid());
//                MarketCategory marketCategory=marketCategoryMapper.selectOneByExample(marketCategoryExample2);
//                if(!filterCategoryList.contains(marketCategory)) {
//                    filterCategoryList.add(marketCategory);
//                }
//
//            }
//
//        }
        //二级商品类目下有商品的返回
//        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
//        List<MarketCategory> filterCategoryList = new ArrayList<>();
//        List<MarketCategory> categoryList=marketCategoryMapper.selectByExample(marketCategoryExample);
//        for (int i = 0; i < categoryList.size(); i++) {
//            MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
//            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
//            //有商品就返回
//            if(categoryList.get(i).getPid()!=0){
//                criteria.andCategoryIdEqualTo(categoryList.get(i).getId());
//                List<MarketGoods> marketGoods=marketGoodsMapper.selectByExample(marketGoodsExample);
//                if(marketGoods!=null&&marketGoods.size()!=0){
//                    filterCategoryList.add(categoryList.get(i));
//                }
//            }
//        }
//
//
//        return filterCategoryList;
//    }
        List<MarketCategory> filterCategoryList = new ArrayList<>();
//        if (StringUtils.isEmpty(goodBo.getKeyword()) && StringUtils.isEmpty(goodBo.getCategoryId())) {
        if (Integer.valueOf(goodBo.getCategoryId())!=0&&StringUtils.isEmpty(goodBo.getKeyword())) {
            //查出一级类目id
            MarketCategoryExample marketCategoryExampleL2Cur = new MarketCategoryExample();
            MarketCategory marketCategoryL2Cur = marketCategoryMapper.selectByPrimaryKey(Integer.valueOf(goodBo.getCategoryId()));
            //查出一级类目为marketCategoryL2Cur的所有二级类目
            MarketCategoryExample marketCategoryExampleL2 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteriaL2 = marketCategoryExampleL2.createCriteria();
            criteriaL2.andPidEqualTo(marketCategoryL2Cur.getPid());
            List<MarketCategory> marketCategoryListL2 = marketCategoryMapper.selectByExample(marketCategoryExampleL2);
            //查这些类目下有商品没 有就加入
//        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();


            for (int i = 0; i < marketCategoryListL2.size(); i++) {
                MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
                MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
                //有商品就返回
                if (marketCategoryListL2.get(i).getPid() != 0) {
                    criteria.andCategoryIdEqualTo(marketCategoryListL2.get(i).getId());
                    List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
                    if (marketGoods != null && marketGoods.size() != 0) {
                        filterCategoryList.add(marketCategoryListL2.get(i));
                    }
                }
            }
        } else {
            //根据关键词去查找商品，找到这些商品的类目id，找到类目id
            List<MarketGoods> goodsList = goodsList1(goodBo);
            for (int i = 0; i < goodsList.size(); i++) {
                MarketCategory marketCategory = new MarketCategory();
                MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
                criteria.andIdEqualTo(goodsList.get(i).getCategoryId());
                marketCategory = marketCategoryMapper.selectOneByExample(marketCategoryExample);
                if (!filterCategoryList.contains(marketCategory)) {
                    filterCategoryList.add(marketCategory);
                }
            }

        }


        return filterCategoryList;
    }


}
